<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>鼓励金购买</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../css/style.css"/>
		<link rel="stylesheet" href="../css/iconfont.css" />
		<link rel="stylesheet" href="../css/wallet.css" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
		    <a class=" mui-icon mui-icon-left-nav mui-pull-left" href="JavaScript:history.go(-1)"></a>
		    <h1 class="mui-title">鼓励金购买</h1>
		</header>
		<div class="mui-content recharge_content">
			<div class="recharge_top">
				<ul class="recharge_lis clearfix">
					<li>
						<p class="style">余额</p>
						<p class="num" id="yue">0</p>
					</li>
					<li>
						<p class="style">鼓励金</p>
						<p class="num" id="score">0</p>
					</li>
				</ul>
			</div>
		    <div class="exchange_lisbox">
		    	<ul class="mui-table-view mui-grid-view mui-grid-9 eul clearfix">
		    		<li>
		    			<div data-value="100" class=" eli">
		    				<p>100元</p>
		    				<p class="sxjif">1000鼓励金</p>
		    			</div>
		    		</li>
		    		<li>
		    			<div data-value="200" class=" eli">
		    				<p>200元</p>
		    				<p class="sxjif">2000鼓励金</p>
		    			</div>
		    		</li>
		    		<li>
		    			<div data-value="500" class=" eli">
		    				<p>500元</p>
		    				<p class="sxjif">5000鼓励金</p>
		    			</div>
		    		</li>
		    		<li>
		    			<div data-value="1000" class=" eli">
		    				<p>1000元</p>
		    				<p class="sxjif">10000鼓励金</p>
		    			</div>
		    		</li>
		    		<li>
		    			<div data-value="2000" class=" eli">
		    				<p>2000元</p>
		    				<p class="sxjif">20000鼓励金</p>
		    			</div>
		    		</li>
		    		<li>
		    			<div data-value="5000" class=" eli">
		    				<p>5000元</p>
		    				<p class="sxjif">50000鼓励金</p>
		    			</div>
		    		</li>
		    		<li>
		    			&nbsp;
		    		</li>
		    	</ul>
		    	
		    </div>
		    <button class="wallet_btn" id='quren' style="width: 100%;"><lable style='color: #fff;'>确认</lable></button>
		    <div class="exchange_tip">
		    	<div class="exchange_bt"><span>温馨提示</span></div>
		    	<p>100元可购买1000鼓励金，1元人民币=10鼓励金，100元起充。</p>
		    </div>
		</div>
		<input type="hidden" id="pay_money" />
		<input type="hidden" id='order_sn'>
	</body>

</html>
<script src="../js/mui.min.js"></script>
<script src="../js/zepto.min.js"></script>
<script type="text/javascript" src="../js/date.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/layer/layer.min.js"></script>
<script type="text/javascript" src="../js/app.js"></script>
<script>
	$(function() {
		//记录扫码客户端，微信，或支付宝
		var pay_type = 0;
		var ua = window.navigator.userAgent.toLowerCase();
		var auth_code = getQueryString("auth_code");
		var member ="";
		if(ua.match(/MicroMessenger/i) == 'micromessenger') {
			pay_type = 2;
		} else if(ua.match(/AlipayClient/i) == 'alipayclient') {
			pay_type = 3;
			if(auth_code == null) {
				var fromurl = location.href;
				var url = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2016030801195057&scope=auth_base&redirect_uri='+encodeURIComponent(fromurl);
				location.href = url;
			}
		}
		//获取商户信息
		$.ajax({
			async: false, 
			url: APP.api_url + 'wallet/get_storeby_id',
			data: {
				store_id: localStorage.getItem("user_agent_id"),
			},
			success: function(data) {
				if(data.code == 200) {
					
					localStorage.setItem("cut_store_info", JSON.stringify(data.data)); 
					$('#yue').text(data.data.store_balance);
					$('#score').text(data.data.score);
					$('.fuk_spnam').text(data.data.store_name);
					//图片地址
					var picture_url = user_tupian(data.data.store_logo,'../images/cpimg.jpg');
					$("#store_thum").attr('src', picture_url);
				} else {
					layer.msg('该店铺不存在');
					return false;
				}
			}
		})
		//确认付款
		$("#quren").click(function() {
			$("#quren").attr("disabled", true);
			$("#quren").css("background-color","#C3BCB8");
			
			//付款金额
			var pay_money = $("#pay_money").val();
			if(pay_money == "") {
					layer.msg('请选择购买金额');
					$("#quren").attr("disabled", false);
					$("#quren").css("background-color","#FF7831");
					return false;
			}
			//获取店铺信息
			var store_info = JSON.parse(localStorage.getItem("cut_store_info"));
			if(pay_type == 3){
				//获取支付宝key
				alipay_user(auth_code,getUrlParam("fromuid"));
			}else if(pay_type == 2){
				//获取openid
				var openid = localStorage.getItem('openid');
				//获取用户信息
				member_by_openid(openid,store_info['u_id']);
			}
			var member = JSON.parse(localStorage.getItem('member'));
			
			var act = "alipay";
			var jstype = "html";
			if(pay_type == 2) {
				act = "wechat";
				jstype = "jsonp";
			}
			//订单编号
			$('#order_sn').val(getSn());
			$.ajax({
				url: APP.api_url + 'Payment/' + act,
				data: {
					WIDout_trade_no	: $("#order_sn").val(),
					WIDsubject		: "二维码付款",
					WIDtotal_fee	: pay_money,
					//WIDtotal_fee	: 0.01,
					WIDshow_url		: 'www',
					WIDbody			: "二维码付款",
					uid				: getUid(),
					order_ids		: store_info['id'],
					type			: "cz_score",
					user_type		: 1,
					token			: get_token(),
					openid			: member['openid'],
					pay_type		: pay_type
				},
				type: "post",
				dataType: jstype,
				jsonp: 'callback',
				jsonpCallback: 'jsonpCallback',
				success: function(data) {
					if(pay_type == 2) {
						callpay(data);
					} else {
						$("#callback").append(data);
					}
				}
			})
		});
		
		$(".eli").click(function(){
			$(".eul").find(".active").removeClass("active");
			$(this).addClass("active");
			$("#pay_money").val($(this).data("value"));
		})
		
		//店铺链接
		$('#store_thum').click(function(){
			var store_info = JSON.parse(sessionStorage.getItem("cut_store_info"));
			
			var index = "shop.html?";
			if(store_info.template_id &&  store_info.template_id!="") {
				index = "shop" + store_info.template_id + ".html?";
			}
			location.href = APP.web_root+index+'store_id='+store_info['id']+'&fromuid='+getUrlParam("store_id");
		});
		
	});
	
	//只能数字和小数点
	function clearNoNum(obj) {
		obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
		obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
		obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
		obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	}
	//调用微信JS api 支付
	function jsApiCall(jsApiParameters) {
		WeixinJSBridge.invoke('getBrandWCPayRequest', jsApiParameters,
			function(res) {
				if(res.err_msg == "get_brand_wcpay_request:ok") {
					layer.msg('购买成功！', {
						  time: 2000 
						}, function(){
							location.href=APP.web_root+'shopwallet/index.html';
						});
				} else {
					layer.msg('付款失败');
					$("#quren").attr("disabled",false);
					$("#quren").css("background-color","#FF7831");
				}
			});
	}

	function callpay(jsApiParameters) {
		console.log(jsApiParameters);
		if(typeof WeixinJSBridge == "undefined") {
			if(document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', jsApiCall(jsApiParameters), false);
			} else if(document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', jsApiCall(jsApiParameters));
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall(jsApiParameters));
			}
		} else {
			jsApiCall(jsApiParameters);
		}
	}
	function jsonpCallback(data) {
		$("#cut_back").append(data);
	}
</script>
